//
//  LGTeamRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/16.
//  申请记录

#import "LGTeamRecordController.h"

//团队详情
#import "LGTeamDetailController.h"
//个人资料
#import "LGPersionalDataController.h"
#import "LGTeamRecordCell.h"

@interface LGTeamRecordController ()<UITableViewDelegate,UITableViewDataSource>

/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
@end

@implementation LGTeamRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"申请记录";
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/my.teams/teamsApplyList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:@"暂无申请记录" offset:0];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        self.isfirst = NO;
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:LGErrorMessage offset:0];
        }
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGTeamRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    cell.checkApplyAction = ^(NSString * _Nonnull teamid, NSString * _Nonnull userid, NSString * _Nonnull status) {
        __weak typeof(self) weakSelf = self;
        [RequestUtil withPOST:@"/doctor/my.teams/applyMember" parameters:@{@"teamid":teamid,@"userid":userid,@"status":status} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [TooltipView showMessage:@"审核完成" offset:0];
                [weakSelf requestData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:LGErrorMessage offset:0];
        }];
    };
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    if([tempDic[@"title2"] length]>0){
        //跳转个人资料
        LGPersionalDataController *controller = [[LGPersionalDataController alloc]init];
        controller.userId = LGNSString(tempDic[@"userid"]);
        [self.navigationController pushViewController:controller animated:YES];
        
    }else{
        //跳转团队详情
        LGTeamDetailController *controller = [[LGTeamDetailController alloc]init];
        controller.teamId = LGNSString(tempDic[@"teamid"]);
        [self.navigationController pushViewController:controller animated:YES];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStylePlain];
        [_tableView registerClass:[LGTeamRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(60);
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
